package cn.kevinkun.LeanCloud;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import java.util.Set;

public class HttpUtils {

	public static void doHttpReqeust(final String method, final String url, final Map<String, String> headers,
			final byte[] body, final Callback callback) {

		new Thread(new Runnable() {
			@Override
			public void run() {
				HttpURLConnection connection = null;
				OutputStream outputStream = null;
				try {
					URL u = new URL(url);
					connection = (HttpURLConnection) u.openConnection();
					connection.setDoInput(true);

					connection.setRequestMethod(method);
					connection.setConnectTimeout(5000);
					connection.setReadTimeout(5000);
					connection.setUseCaches(false);

					if (headers != null) {
						Set<Map.Entry<String, String>> sets = headers.entrySet();
						for (Map.Entry<String, String> entry : sets) {
							connection.addRequestProperty(entry.getKey(), entry.getValue());
						}
					}

					if (body != null) {
						connection.setDoOutput(true);
						connection.setFixedLengthStreamingMode(body.length);
						BufferedOutputStream out = new BufferedOutputStream(connection.getOutputStream());
						try {
							out.write(body, 0, body.length);
							out.flush();
						} finally {
							out.close();
						}
					}

					connection.connect();
					int responseCode = connection.getResponseCode();
					InputStream inputStream;
					if (responseCode / 100 == 2) {
						inputStream = connection.getInputStream();
						String result = inputStream2String(inputStream);
						System.out.println("done:" + result);
						if (result != null && callback != null) {
							callback.onSuccess(result);
						}
					} else if (responseCode / 100 == 4) {
						inputStream = connection.getErrorStream();
						String result = inputStream2String(inputStream);
						System.out.println("fail:" + result);
						if (result != null && callback != null) {
							callback.onError(result);
						}
					} else {
						if (callback != null) {
							callback.onError("Error:" + responseCode);
						}
					}

				} catch (final Exception e) {
					e.printStackTrace();
					if (callback != null) {
						callback.onError(e.getMessage());
					}

				} finally {
					if (connection != null) {
						connection.disconnect();
					}
					if (outputStream != null) {
						try {
							outputStream.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}).start();
		;
		// return null;
	}

	private static String inputStream2String(InputStream inputStream) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] bytes = new byte[1024];
		int len = 0;
		try {
			while ((len = inputStream.read(bytes)) != -1) {
				baos.write(bytes, 0, len);
			}
			return new String(baos.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return null;
	}

	public interface Callback {
		void onError(final String error);

		void onSuccess(final String result);
	}

}